package com.vitabuybuy.tv.ui.mine.wallet

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityBillBinding
import com.vitabuybuy.tv.dto.TabEntity
import com.library.common.base.BaseActivity
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.ui.mine.adapter.BillAdapter
import com.vitabuybuy.tv.ui.mine.wallet.viewmodel.MyWalletViewModel
import kotlinx.android.synthetic.main.activity_bill.*
import java.util.ArrayList

class BillActivity : BaseActivity<MyWalletViewModel, ActivityBillBinding>() {


    private val billAdapter by lazy(LazyThreadSafetyMode.NONE) { BillAdapter() }

    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_bill

//    private var startTime = ""
//    private var endTime = ""
    private val mTabEntities = ArrayList<CustomTabEntity>()
    private var pageNumber = 1
    private var type =""

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.bill))
        mBinding.viewModel = viewModel
//        val list1 = mutableListOf(
//            getString(R.string.all),
//            getString(R.string.withdraw),
//            getString(R.string.expense)
//        )
//        val arrayAdapter = ArrayAdapter(this, R.layout.item_spinner_2, list1)
//        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
//        spnType.adapter = arrayAdapter
        mTabEntities.add(TabEntity(getString(R.string.income), 0, 0))
        mTabEntities.add(TabEntity(getString(R.string.expense), 0, 0))
        tlBiddingClass.setTabData(mTabEntities)
        listView.apply {
            adapter = billAdapter
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 0))
        }
        type = getString(R.string.income)
    }

    override fun observeData() {
        viewModel.getBillList(type,pageNumber)
        viewModel.billList.observe(this, Observer {
            if(pageNumber == 1){
                billAdapter.setNewData(it)
            }else{
                billAdapter.addData(it)
            }
            if (billAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
        })
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber ++
                viewModel.getBillList(type,pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getBillList(type,pageNumber)
            }
        })
        tlBiddingClass.setOnTabSelectListener(object : OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                when(position){
                    0->{
                        type = getString(R.string.income)
                    }
                    1->{
                        type = getString(R.string.expense)
                    }
                }
                pageNumber = 1
                viewModel.getBillList(type,pageNumber)
            }
            override fun onTabReselect(position: Int) {
            }
        })
//        rlDate.setOnClickListener {
//            ChooseTimeActivity.openActivity(mContext, 10001)
//        }
    }

    override fun handleEvent(msg: MessageModel) {
        when(msg.code){
            0->{
                pageNumber = 1
                viewModel.getBillList(type,pageNumber)
            }
            1->{
                refresh.onLoad(msg.arg1)
            }
        }
    }
    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, BillActivity::class.java)
        }
    }
}
